var _SERVER_URL = 'https://ejoy.wanxuantong.cn/index.php?r=api/data';


function conLog(data) {
  console.log(data);
}

/*var obj = {'id':'1','title':'测试','content':'内容'},param="";

for(var i in obj) {
	if(param!="")
		param+="&";
     param+=i+"="+encodeURIComponent(obj[i]);
	
}
console.log(param);*/

function httpGet(data, sucFun) {
  wx.getStorage({
    key: 'user_id',
    success: function (res) {
      data.user_id = res.data;
      wx.request({
        url: _SERVER_URL,
        data: data,
        header: {
          'content-type': 'application/json'
        },
        success: sucFun
      });
    }, fail: function () {
      wx.request({
        url: _SERVER_URL,
        data: data,
        header: {
          'content-type': 'application/json'
        },
        success: sucFun
      });
    }
  });
}



function mergeJsonObject(jsonbject1, jsonbject2, rs) {
  var resultJsonObject = {};
  if (!rs) {
    for (var attr in jsonbject1) {
      console.log(attr);
      resultJsonObject[attr] = jsonbject1[attr];
    }
    for (var attr in jsonbject2) {
      resultJsonObject[attr] = jsonbject2[attr];
    }
  } else {
    var i = 0;
    for (var attr in jsonbject1) {
      resultJsonObject[i] = jsonbject1[attr];
      i++;
    }
    for (var attr in jsonbject2) {
      resultJsonObject[i] = jsonbject2[attr];
      i++;
    }
  }
  return resultJsonObject;
}

function autoLogin() {
  wx.getStorage({
    key: 'session_key',
    success: function (res) {
      if (!res.data) {
        //console.log('未登录');
        userLogin();
      } else {
        //console.log('已登录');
      }
    },
    fail: function () {
      userLogin();
    }
  });
}

function userLogin() {
  //console.log("初始化成功！");
  wx.login({
    success: function (res) {
      if (res.code) {
        wx.getUserInfo({
          withCredentials: false,
          success: function (res2) {
        	wx.setStorage({
                key: "nickname",
                data: res2.userInfo.nickName
            });
            wx.setStorage({
                key: "avatarUrl",
                data: res2.userInfo.avatarUrl
            });  
            //登录
            httpGet({
              action: 'login',
              code: res.code,
              headimgurl: res2.userInfo.avatarUrl,
              nickname: res2.userInfo.nickName,
              sex: res2.userInfo.gender,
              location: res2.userInfo.country + ' ' + res2.userInfo.province + ' ' + res2.userInfo.city
            }, function (res) {
              if (res.data.openid) {
                //console.log("登录成功！");
                wx.setStorage({
                  key: "openid",
                  data: res.data.openid
                });
                wx.setStorage({
                  key: "user_id",
                  data: res.data.user_id
                });
                console.log(res.data);
                wx.setStorage({
                  key: "invite_code",
                  data: res.data.invite_code
                });
                wx.setStorage({
                  key: "session_key",
                  data: res.data.session_key
                });
                wx.setStorage({
                    key: "mobile",
                    data: res.data.mobile
                });
                
              } else {
                //console.log("登录失败");
                //console.log(res.data);
              }
            });
          },fail(res){
            console.log(res);
          }
        })

      } else {
        //console.log('获取用户登录态失败！' + res.errMsg)
      }
    }
  });
}

function getUserInfo() {
  // wx.getStorage();
}

module.exports = {
  conLog: conLog,
  mergeJsonObject: mergeJsonObject,
  httpGet: httpGet,
  userLogin: userLogin,
  autoLogin: autoLogin,
}


